// pages/studyWord/selectWord/selectWord.js
const app = getApp();
let tool = require('../../../utils/tool.js');
const httpUtils = require('../../../utils/httpUtils.js');

let questData = [{
  "id": "20061",
  "translate": "饥饿的",
  "word": "hungry"
}, {
  "id": "20072",
  "translate": "阿姨",
  "word": "aunt"
}, {
  "id": "20111",
  "translate": "鸡肉",
  "word": "chicken"
}];

Page({
  /**
   * 页面的初始数据
   */
  data: {
    imageurl: app.globalData.imageurl,
    okNums: 0,
    errNums: 0,
    selNums: {},
    ansNums: {},
    flag: false, //显示隐藏提示复习的弹窗的标志
    innerAudioContext: new Object(),
    allWord: '',
    errWord: [],//累计不会的词
    can_notString: "",//不会的词的id字符串
    canClick: 0,
    questData: [],
    wordID: 0,
    wordLen: 0,
    readTimes: {},
    page: 1,
    pageSize: 20,
    hasMoreData: false,
    packageid: "",
    isHavePage: true//下一页的按钮true显示false隐藏
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.data.innerAudioContext = wx.createInnerAudioContext()
    this.data.innerAudioContext.autoplay = false;
    this.data.innerAudioContext.loop = false;
    this.data.packageid = options.packageid;
    var studyadata = JSON.parse(options.studyData);
    var isHave = studyadata.length < this.data.pageSize ? false : true;
    that.setData({
      questData: studyadata,
      isHavePage: isHave,

    });


  },

  /**
   * 获取学前检测的单词
   */
  getSelectWord: function () {
    let that = this;
    wx.showLoading({
      title: '',
      mask: true
    })
    httpUtils.getSelectData({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      package_id: this.data.packageid,
      page: this.data.page,
      size: this.data.pageSize,
    }).then((res) => {
      console.log(res);
      let isHave = res.data.to_study.length < this.data.pageSize ? false : true;
      that.setData({
        isHavePage: isHave,
        questData: res.data.to_study,
        ansNums: {},
      })
      // 控制滚动,让界面滑动到顶部
      wx.pageScrollTo({
        scrollTop: 0,
        duration: 300
      })
    });
  },

  /**
    * 提交单词 点击下一页和开始识记都需要提交 isEnd false 下一页 true开始识记
    */
  referWord: function (isEnd) {
    let that = this;
    //单词id以，隔开的字符串
    var canString = "";
    var can_notString = "";
    for (var key in this.data.ansNums) {
      if (this.data.ansNums[key] == 1) {
        if (key < this.data.questData.length) {
          canString += this.data.questData[key].id + ",";
        }
      }
    }
    let f_type = 0;
    if (isEnd) {
      f_type = 1;
      can_notString = this.data.can_notString;
    } else {
      f_type = 0;
      can_notString = "";
      if (canString == "") {//没有一眼会的 更新不会的单词的list然后直接请求下一页数据
        for (var key in this.data.ansNums) {

          if (this.data.ansNums[key] == 2) {
            if (key < this.data.questData.length) {
              this.data.errWord.push(this.data.questData[key]);
            }
          }
        }
        //请求下一页单词
        that.data.page++;
        that.getSelectWord();
        return;
      }
    }
    console.log(canString);
    wx.showLoading({
      title: '',
      mask: true
    })

    httpUtils.referWord({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      package_id: this.data.packageid,
      type: "init",
      times: "1",
      f_type: f_type,
      can: canString,
      can_not: can_notString,
    }).then((res) => {
      console.log(res);
      if (res.data.is_finish != 0) {//不等于0的时候跳结业报告
        /**
         * 跳结业报告
         */
        let urlStr = encodeURIComponent(res.data.url);
        wx.redirectTo({
          url: '/pages/web/web?url=' + urlStr,
        })
        return;
      }
      //提交成功 对全局变量的改变都放提交成功后 避免提交不成功就改变值 再提交就有重复数据
      for (var key in this.data.ansNums) {
        if (this.data.ansNums[key] == 2) {
          if (key < this.data.questData.length) {
            this.data.errWord.push(this.data.questData[key]);
          }
        }
      }
      console.log(this.data.errWord);

      if (isEnd) {//开始识记的 先判断有没有不会的; 有就跳识记界面 没有就返回
        if (this.data.errWord.length == 0) {
          //返回
          wx.navigateBack({
            delta: 1
          })
        } else {
          let studydata = JSON.stringify(this.data.errWord);
          wx.redirectTo({
            url: '/pages/studyWord/memorizeWord/memorizeWord?packageid=' + this.data.packageid + '&studyData=' + studydata,
          })
        }
      } else {
        //请求下一页单词
        this.data.page++;
        that.getSelectWord();
      }
    });
  },
  /**
   *打印单词
   */
  prints: function () {
    let that = this;
    wx.showLoading({
      title: '',
      mask: true
    })
    httpUtils.prints({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      package_id: this.data.packageid,
      from: "word_remember",
      question_type: 0,
      word_list: this.data.can_notString,
    }).then((res) => {
      console.log(res);
      //打印成功提交单词
      that.referWord(true);
    });
  },

  clickErr: function (e) {
    if (this.data.errNums >= 45) {
      wx.showToast({
        title: '单词已选够45个了，开始识记吧',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    let temWrod = e.currentTarget.dataset.words;
    let temA = this.data.ansNums;
    let errNums = this.data.errNums;
    let okNums = this.data.okNums;
    if (temA[temWrod] == 1) {
      okNums--;
    }
    if (temA[temWrod] != 2) {
      errNums++;
    }
    temA[temWrod] = 2;

    this.setData({
      ansNums: temA,
      errNums: errNums,
      okNums: okNums
    })
  },
  clickOk: function (e) {
    // console.log("-----------------", JSON.stringify(e));
    let temWrod = e.currentTarget.dataset.words;
    let errNums = this.data.errNums;
    let okNums = this.data.okNums;
    let temA = this.data.ansNums;
    if (temA[temWrod] == 2) {
      errNums--;
    }
    // okNums++;

    if (temA[temWrod] != 1) {
      okNums++;
    }

    temA[temWrod] = 1;
    this.setData({
      ansNums: temA,
      errNums: errNums,
      okNums: okNums
    })

  },
  clickTxt: function (e) {
    this.data.innerAudioContext.stop();
    let temWrod = e.currentTarget.dataset.words;
    let temNum = this.data.selNums;
    // let audiosrc = app.globalData.voicepath + temWrod + ".mp3";
    let audiosrc=e.currentTarget.dataset.audios;
    this.data.innerAudioContext.src = audiosrc;
    this.data.innerAudioContext.play();
    var isHas = false;//是否点击过 false没有点击过
    for (var key in temNum) {//0正常样式1英文变粗 2英文变粗出中文
      if (key == temWrod) {
        if (temNum[key] < 2) {
          temNum[key]++;
        } else {
          temNum[key] = 1;
        }
        isHas = true;
      } else {
        temNum[key] = 0;
      }
    }
    if (!isHas) {
      temNum[temWrod] = 1;
    }
    // if (temNum[temWrod] === undefined) {
    //   temNum[temWrod] = 1;
    //   this.setData({
    //     selNums: temNum,
    //     word1Cls: 'xhjc_text3_2'
    //   });
    // } else {
    //   let rTimes = this.data.readTimes;
    //   rTimes[temWrod] = 1;
    //   if (temNum[temWrod] < 2) {
    //     temNum[temWrod]++;
    //   } else {
    //     temNum[temWrod] = 1;
    //   }
    this.setData({
      selNums: temNum,
      // readTimes: rTimes
    });
    console.log(this.data.selNums);
  },

  /**
   * 跳转 单词识记界面
   */
  gotostudy: function () {
    var that = this;
    let arr = Object.keys(this.data.ansNums);
    this.data.can_notString = "";
    for (let i = 0; i < this.data.errWord.length; ++i) {
      this.data.can_notString += this.data.errWord[i].id + ",";
    }

    for (var key in this.data.ansNums) {

      if (this.data.ansNums[key] == 2) {
        if (key < this.data.questData.length) {
          this.data.can_notString += this.data.questData[key].id + ",";
        }
      }
    }
    console.log(this.data.can_notString);
    if (arr.length == 0 && this.data.can_notString == "") {
      //没有不会的单词不提交打印 直接提交
      //弹提示先选择单词再开始识记
      wx.showToast({
        title: '还没有选词哦~~',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    if (this.data.can_notString == "") {
      //不提交打印，直接提交单词
      that.referWord(true);
    } else {
      //提交打印
      that.prints();
    }
  },

  //下一页
  nextPage: function () {
    let arr = Object.keys(this.data.ansNums);
    console.log(this.data.questData.length + "," + arr);
    if (arr.length != this.data.questData.length) {
      wx.showToast({
        title: '当前页还没全部选词，不能进入下一页~~',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    // 提交当前页的一眼会的词 然后获取学前检测数据
    let that = this;
    that.referWord(false);
  },

  //关闭弹窗
  closeMask: function () {
    this.setData({
      flag: true,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})